package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.UserService;

import java.util.*;

@Transactional
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByusername(String username) {
        User user= userDao.findByusername(username);
        return user;
    }

    //动态展示系统的菜单
    @Override
    public List<Map> getMenuList(String username) {
        List<Map> list = new ArrayList<>();

        Map one = new HashMap<>();
        Map menuMap = new HashMap<>();
        menuMap.put("username", username);
        one.put("path", "1");
        one.put("title", "工作台");
        one.put("icon", "fa-dashboard");
        one.put("children", null);
        list.add(one);
        Set<Integer> menuIds = userDao.findMenuIdsByUsername(username);
        for (Integer menuId : menuIds) {
            Map map = userDao.findMenuByMenuId(menuId);
            if(map!=null){
                menuMap.put("menuId", menuId);
                menuMap.put("level", 2);
                List<Map> list2 = userDao.findMenuBymenuMap(menuMap);
                menuMap.put("level", 3);
                for (Map m : list2) {
                    menuMap.put("menuId", m.get("id"));
                    List<Map> list3 = userDao.findMenuBymenuMap(menuMap);
                    m.put("children", list3);
                }
                map.put("children", list2);
                list.add(map);
            }


        }
        return list;
    }
    /**
     * 添加系统操作用户
     * @param user
     */
    @Override
    public void addUser(User user, int[] roleIds) {
        userDao.add(user);
        //循环角色ids 和 用户id 插入中间表中
        setUserAndRole( user.getId(),roleIds);
    }

    /**
     * //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
     */
    public void setUserAndRole(Integer userId,int[] roleIds){
        if (roleIds !=null && roleIds.length >0){
            HashMap<String,Integer> hm = new HashMap();
            for (int roleId : roleIds) {
                hm.put("user_id", userId);
                hm.put("role_id", roleId);
                userDao.addUserRole(hm);
            }
        }
    }

    /**
     * 修改系统用户
     * @param user
     * @param roleIds
     */
    @Override
    public void edit(User user, int[] roleIds) {
        //1.修改用户数据
        userDao.edit(user);
        if(roleIds != null && roleIds.length >0){
            //2.根据检查组id 删除检查组和检查项中间表数据
            Integer userId = user.getId();
            userDao.deleteUserRoleByUserId(userId);
            //3.重新建立关联关系
            setUserAndRole(userId,roleIds);
        }
    }


    /**
     * 用户分页显示
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page= userDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public User findUserById(Integer id) {
        return userDao.findUserById(id);
    }




    /**
     * 根据id删除系统用户
     * @param id
     */
    @Override
    public void delete(Integer id) throws RuntimeException {
        if(id==1){
            throw new RuntimeException("不能删除超级管理员,不能删除,无效操作!");
        }
        int roleUserIds= userDao.findRoleUserByUserId(id);
        if(roleUserIds>0){
            throw new RuntimeException("存在与角色数据关联,不能删除,无效操作!");
        }
        userDao.deleteById(id);
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }
}
